package cn.nexd.app.indoor.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;
import cn.nexd.app.indoor.R;
import cn.nexd.app.indoor.adapter.MallListAdapter;
import cn.nexd.app.indoor.application.MyApplication;
import cn.nexd.app.indoor.model.FloorModel;
import cn.nexd.app.indoor.model.MallModel;
import cn.nexd.app.indoor.presenter.MallListPresenter;
import cn.nexd.app.indoor.ui.iView.IMallListActivityView;
import cn.nexd.app.indoor.util.LogUtil;

/**
 * Created by wangxu on 16/7/19.
 */
public class MallListActivity extends BaseActivity<MallListPresenter> implements IMallListActivityView {

    private static final long DELAY = 1000;
    public long lastClickTime = 0;

    @Bind(R.id.lv_malls)
    ListView lvMalls;

    MallListAdapter mallListAdapter;

    @Bind(R.id.map_top_layout)
    View viewTop;

    @Bind(R.id.maplayout_title)
    TextView tvTopTitle;

    @Bind(R.id.maplayout_back)
    ImageView ivTopBack;

    @Bind(R.id.maplayout_search)
    ImageView ivSearch;

    @Bind(R.id.maplayout_home)
    ImageView ivAboutMe;

    @Bind(R.id.config)
    ImageView ivConfig;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {
        presenter = new MallListPresenter(this, this);
        presenter.init();
    }

    @Override
    public void initView() {
        initTopLayout();
        initData();
    }

    private void initTopLayout() {
        tvTopTitle.setText("商场列表");
        ivTopBack.setVisibility(View.GONE);
        ivSearch.setVisibility(View.GONE);
        ivAboutMe.setVisibility(View.GONE);
        if (MyApplication.IS_TEST_MODE) {
            ivConfig.setVisibility(View.VISIBLE);
        } else {
            ivConfig.setVisibility(View.GONE);
        }
    }

    private void initData() {
        // 从上一个页面,或者是从Application中获取商城名称列表的数据.
        mallListAdapter = new MallListAdapter(this);
        mallListAdapter.addMallList(MyApplication.mallList);
        lvMalls.setAdapter(mallListAdapter);
        setLvMallListener();
    }


    //防止连击
    private void setLvMallListener() {
        lvMalls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastClickTime > DELAY) {
                    lastClickTime = currentTime;
                } else {
                    return;
                }
                presenter.setMallModel(mallListAdapter.getItem(position));
                presenter.checkIsExistRootFile();
            }
        });
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(MallListActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.config)
    public void onClick(View view) {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    @Override
    public void showProgress(int visibility, String msg) {
    }

    @Override
    public void intoNextActivity(MallModel mallModel, ArrayList<FloorModel> floorModels) {
        Intent intent = new Intent(MallListActivity.this, NexdMapActivity.class);
        intent.putExtra("MallModel", mallModel);
        intent.putParcelableArrayListExtra("FloorModels", floorModels);
        startActivity(intent);
    }

    @Override
    public void printLog(String tag, String msg, int type) {
        LogUtil.printLog(tag, msg, type);
    }
}
